package apkg;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Servlet
 */
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@EJB ServletBean bean;
       
    public Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	// Check bank account amount
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.print("got GET request!"); 
		out.print("Current amount: " + bean.getAmount());
	}

	// Create bank account
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.print("got POST request!");
		String firstname = request.getParameter("firstname");
		String familyname = request.getParameter("familyname");
		String reggie = request.getParameter("reggie");
		String codebase = request.getParameter("codebase");
		bean.setup("http://localhost:8080", "jini://localhost", firstname, familyname, 2300);
		bean.registerWithBank();
	}

	// Change bank account amount
	protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.print("got PUT request!");
		String amount = request.getParameter("amount");
		bean.changeAmount(amount);
	}

	// 
	protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.print("got DELETE request!");
		bean.changeAmount("-" + bean.getAmount());
	}

}
